import Joi from 'joi';
import debugCheck from '@kuiper-helpers/debug-check';
import * as validation from '../../common/validation';
import findManyFilter from '../../common/validation/findManyFilter';
import joiOmit from '../helpers/joiOmit';

export const pagination = {
	tags: ['api', 'blacklist'],
	description: '黑名单列表-分页查看',
	auth: debugCheck('disable-open-api-auth')
		? false
		: {
				mode: 'required',
				access: {
					// 只允许使用 URL Token 访问
					entity: 'app',
				},
		  },

	validate: {
		payload: Joi.object({
			page: Joi.number().default(1).description('当前页'),
			perPage: Joi.number().default(10).description('每页条目数'),
			filter: findManyFilter.blacklist.append({
				createTimeGTE: Joi.date().description('开始时间。大于等于'),
				createTimeLTE: Joi.date().description('开始时间。小于等于'),
			}),
		}),
	},

	response: {
		schema: Joi.object({
			data: Joi.object({
				BlacklistPagination: Joi.object({
					count: Joi.number().description('总条数'),
					pageInfo: Joi.object({
						currentPage: Joi.number().description('当前页数'),
						perPage: Joi.number().description('每页条数'),
						pageCount: Joi.number().description('总页数'),
						itemCount: Joi.number().description('总条数'),
						hasNextPage: Joi.boolean().description('是否有下一页'),
						hasPreviousPage: Joi.boolean().description('是否有上一页'),
					}),
					items: Joi.array().items(
						validation.blacklistWithId.append({
							user: Joi.object({
								username: Joi.string().description('用户名'),
							}),
						})
					),
				}),
			}),
		}).label('Response'),
	},

	handler: {
		injectGraphql: {
			payloadBuilder(request) {
				const {
					payload: { page, perPage, filter },
				} = request;

				return JSON.stringify({
					variables: { page, perPage, filter },
					// #region query
					query: `
						query($page: Int = 1, $perPage: Int = 10, $filter: FilterFindManyBlacklistInput, $sort: SortFindManyBlacklistInput = _ID_DESC) {
							BlacklistPagination(page: $page, perPage: $perPage, filter: $filter, sort: $sort) {
								count
								pageInfo {
									currentPage
									perPage
									pageCount
									itemCount
									hasNextPage
									hasPreviousPage
								}
								items {
									_id
									target
									uids
									taskCategories
									note
									enable
									createTime
									updateTime
									createBy
								}
							}
						}`,
					// #endregion
				});
			},
		},
	},
};

export const findById = {
	tags: ['api', 'blacklist'],
	description: '根据ID查询监测黑名单',
	auth: debugCheck('disable-open-api-auth')
		? false
		: {
				mode: 'required',
				access: {
					// 只允许使用 URL Token 访问
					entity: 'app',
				},
		  },

	validate: {
		params: Joi.object({
			blacklistId: Joi.string(),
		}),
	},

	response: {
		schema: Joi.object({
			data: Joi.object({
				BlacklistById: validation.blacklistWithId,
			}),
		}).label('Response'),
	},

	handler: {
		injectGraphql: {
			payloadBuilder(request) {
				const {
					params: { blacklistId },
				} = request;

				return JSON.stringify({
					variables: { _id: blacklistId },
					// #region query
					query: `
						query($_id: MongoID!) {
							BlacklistById(_id: $_id) {
								_id
								target
								uids
								taskCategories
								note
								enable
								createTime
								updateTime
								createBy
							}
						}`,
					// #endregion
				});
			},
		},
	},
};

export const count = {
	tags: ['api', 'blacklist'],
	description: '监测黑名单数量统计',
	auth: debugCheck('disable-open-api-auth')
		? false
		: {
				mode: 'required',
				access: {
					// 只允许使用 URL Token 访问
					entity: 'app',
				},
		  },

	validate: {
		payload: Joi.object({
			filter: findManyFilter.blacklist,
		}),
	},

	response: {
		schema: Joi.object({
			data: Joi.object({
				BlacklistCount: Joi.number().description('数量'),
			}),
		}).label('Response'),
	},

	handler: {
		injectGraphql: {
			payloadBuilder(request) {
				const {
					payload: { filter },
				} = request;

				return JSON.stringify({
					variables: { filter },
					// #region query
					query: `
						query($filter: FilterCountBlacklistInput) {
							BlacklistCount(filter: $filter)
						}`,
					// #endregion
				});
			},
		},
	},
};

export const removeById = {
	tags: ['api', 'blacklist'],
	description: '删除黑名单',
	auth: debugCheck('disable-open-api-auth')
		? false
		: {
				mode: 'required',
				access: {
					// 只允许使用 URL Token 访问
					entity: 'app',
				},
		  },

	validate: {
		params: Joi.object({
			blacklistId: Joi.string().required(),
		}),
	},

	response: {
		schema: Joi.object({
			data: Joi.object({
				BlacklistRemoveById: Joi.object({
					recordId: Joi.string(),
				}).allow(null),
			}),
		}).label('Response'),
	},

	handler: {
		injectGraphql: {
			payloadBuilder(request) {
				const {
					params: { blacklistId },
				} = request;

				return JSON.stringify({
					variables: { blacklistId },
					// #region query
					query: `
						mutation($blacklistId: MongoID!) {
							BlacklistRemoveById(_id: $blacklistId) {
								recordId
							}
						}`,
					// #endregion
				});
			},
		},
	},
};

export const removeMany = {
	tags: ['api', 'blacklist'],
	description: '删除黑名单',
	auth: debugCheck('disable-open-api-auth')
		? false
		: {
				mode: 'required',
				access: {
					// 只允许使用 URL Token 访问
					entity: 'app',
				},
		  },

	validate: {
		payload: Joi.array().items(Joi.string()).required(),
	},

	response: {
		schema: Joi.object({
			data: Joi.object({
				BlacklistRemoveMany: Joi.object({
					numAffected: Joi.number(),
				}),
			}),
		}).label('Response'),
	},

	handler: {
		injectGraphql: {
			payloadBuilder(request) {
				const { payload } = request;

				return JSON.stringify({
					variables: { _ids: payload },
					// #region query
					query: `
						mutation($_ids: [MongoID]!) {
							BlacklistRemoveMany(filter: { _ids: $_ids }) {
								numAffected
							}
						}`,
					// #endregion
				});
			},
		},
	},
};

export const updateById = {
	tags: ['api', 'blacklist'],
	description: '更新黑名单',
	auth: debugCheck('disable-open-api-auth')
		? false
		: {
				mode: 'required',
				access: {
					// 只允许使用 URL Token 访问
					entity: 'app',
				},
		  },

	validate: {
		params: Joi.object({
			blacklistId: Joi.string().description('资产监测任务ID'),
		}),
		payload: joiOmit(validation.blacklist),
	},

	response: {
		schema: Joi.object({
			data: Joi.object({
				BlacklistUpdateById: Joi.object({
					recordId: Joi.string().description('更新记录的ID'),
				}),
			}),
		}).label('Response'),
	},

	handler: {
		injectGraphql: {
			payloadBuilder(request) {
				const {
					payload,
					params: { blacklistId },
				} = request;

				return JSON.stringify({
					variables: { record: { ...payload, _id: blacklistId } },
					// #region query
					query: `
						mutation($record: UpdateByIdBlacklistInput!) {
							BlacklistUpdateById(record: $record) {
								recordId
							}
						}`,
					// #endregion
				});
			},
		},
	},
};

export const createOne = {
	tags: ['api', 'blacklist'],
	description: '新建黑名单',
	auth: debugCheck('disable-open-api-auth')
		? false
		: {
				mode: 'required',
				access: {
					// 只允许使用 URL Token 访问
					entity: 'app',
				},
		  },

	validate: {
		payload: validation.blacklist,
	},

	response: {
		schema: Joi.object({
			data: Joi.object({
				BlacklistCreateOne: Joi.object({
					recordId: Joi.string(),
					record: validation.blacklistWithId,
				}).label('BlacklistCreateOne'),
			}).label('Data'),
		}).label('Response'),
	},

	handler: {
		injectGraphql: {
			payloadBuilder(request) {
				const { payload: record } = request;

				return JSON.stringify({
					variables: {
						record,
					},
					// #region query
					query: `
						mutation($record: CreateOneBlacklistInput!) {
							BlacklistCreateOne(record: $record) {
								recordId
								record {
									_id
									target
									uids
									taskCategories
									note
									enable
									createTime
								}
							}
						}`,
					// #endregion
				});
			},
		},
	},
};
